<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 产品列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-circle-plus-outline" class="handle-del mr10"
                    @click="add">新增产品</el-button>
                <el-select clearable v-model="query.shelfId" placeholder="分类">
                    <el-option v-for="item in shelf" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
                <el-input v-model="query.key" placeholder="产品名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="产品名"></el-table-column>
                <el-table-column label="图片(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.img"
                            :preview-src-list="[scope.row.img]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="产品价格">
                    <template slot-scope="scope">￥{{ scope.row.pice}}</template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.canBuy == 1" type="primary" class="handle-del mr10"
                            @click="doOntheshelf(scope.row);">上架</el-button>
                        <el-button v-if=" scope.row.canBuy == 0 " type="danger" class="handle-del mr10"
                            @click=" doOfftheshelf(scope.row) ">下架</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="推荐" align="center">
                    <template slot-scope="scope">
                        <el-button v-if=" scope.row.recommended == 1 " type="danger" class="handle-del mr10"
                            @click=" docancelRecommended(scope.$index, scope.row); ">取消推荐</el-button>
                        <el-button v-if=" scope.row.recommended == 0 " type="primary" class="handle-del mr10"
                            @click=" dosetRecommended(scope.$index, scope.row) ">设为推荐</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit"
                            @click=" handleEdit(scope.$index, scope.row) ">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                            @click=" handleDelete(scope.$index, scope.row) ">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page=" query.pageIndex "
                    :page-size=" query.pageSize " :total=" pageTotal " @current-change=" handlePageChange "></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { getProduct } from '../../api/index';
import { ontheshelf, offtheshelf, deleteProduct, getShopShelf, setRecommended, cancelRecommended } from '../../api/prodect';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                shopId: 0,
                key: '',
                nowPage: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            shelf: [],
            idx: -1,
            id: -1
        };
    },
    created() {
        this.query.shopId = localStorage.getItem('ms_shopId');
        this.getData();
        getShopShelf().then(res => {
            this.shelf = res.data;
        });
    },
    activated() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            getProduct(this.query).then(res => {
                console.log(res);
                this.tableData = res.data.records;
                this.pageTotal = res.data.total;
            });
        },
        docancelRecommended(index, row) {
            cancelRecommended(row.pId).then(e => {
                this.$message.success('取消成功');
                row.recommended = 0
            });
        },
        dosetRecommended(index, row) {
            setRecommended(row.pId).then(e => {
                this.$message.success('推荐成功');
                row.recommended = 1
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'nowPage', 1);
            this.getData();
        },
        //上架
        doOntheshelf(p) {
            ontheshelf(p.pId).then(e => {
                p.pIfSale = 0;
            });
        },
        //下架
        doOfftheshelf(p) {
            offtheshelf(p.pId).then(e => {
                p.pIfSale = 1;
            });
        },
        //删除商品
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    deleteProduct(row.pId).then(e => {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                    });
                })
                .catch(() => { });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.$router.push({ name: 'upload', query: { id: row.pId } });
        },
        //跳转新增
        add() {
            //this.$router.push({ name: 'upload', query: { id: 0 } });
            const { href } = this.$router.resolve({ name: 'upload', query: { id: 0 } });
            window.open(href, '_blank');
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'nowPage', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
